'use strict';

/**
 * 系统配置控制器
 */
const Controller = require('egg').Controller;

class SystemConfigController extends Controller {
  /**
   * 创建系统配置
   * @return {Promise<void>}
   */
  async create() {
    const { ctx, service } = this;
    const configData = ctx.request.body;
    const result = await service.systemConfig.create(configData);
    ctx.success(result);
  }

  /**
   * 查询系统配置列表
   * @return {Promise<void>}
   */
  async index() {
    const { ctx, service } = this;
    const { page = 1, pageSize = 10, ...query } = ctx.query;
    const result = await service.systemConfig.list(query, Number(page), Number(pageSize));
    ctx.success(result);
  }

  /**
   * 根据键查询配置
   * @return {Promise<void>}
   */
  async getByKey() {
    const { ctx, service } = this;
    const { key } = ctx.query;
    const result = await service.systemConfig.findByKey(key);
    ctx.success(result);
  }

  /**
   * 查询单个配置
   * @return {Promise<void>}
   */
  async show() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const result = await service.systemConfig.findById(id);
    ctx.success(result);
  }

  /**
   * 更新系统配置
   * @return {Promise<void>}
   */
  async update() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const configData = ctx.request.body;
    const result = await service.systemConfig.update(id, configData);
    ctx.success(result);
  }

  /**
   * 删除系统配置
   * @return {Promise<void>}
   */
  async destroy() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    await service.systemConfig.destroy(id);
    ctx.success(null, '删除成功');
  }

  /**
   * 更新配置状态
   * @return {Promise<void>}
   */
  async updateStatus() {
    const { ctx, service } = this;
    const { id, status } = ctx.request.body;
    await service.systemConfig.updateStatus(id, status);
    ctx.success(null, '状态更新成功');
  }

  /**
   * 批量获取配置
   * @return {Promise<void>}
   */
  async getConfigsByKeys() {
    const { ctx, service } = this;
    const { keys } = ctx.request.body;
    const result = await service.systemConfig.getConfigsByKeys(keys);
    ctx.success(result);
  }
}

module.exports = SystemConfigController;
